<?php

/**
 * This is the model class for table "cargo_item".
 */
class cargo_item extends ActiveRecord
{
	/**
	 * The followings are the available columns in table 'cargo_item':
	 * @var integer $id
	 * @var integer $cargo_id
	 * @var string $item
	 * @var string $marking
	 * @var string $country
	 * @var integer $qty_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return cargo_item the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cargo_item';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('cargo_id, item, marking, country', 'required'),
			array('cargo_id, qty_id, packages_cargo_id', 'numerical', 'integerOnly'=>true),
			array('cost', 'numerical'),
			array('currency', 'length', 'max'=>10),
			array('item, marking, country', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, cargo_id, item, marking, country, qty_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'detail'=>array(self::HAS_ONE,'cargo_details','foreign_key','condition'=>'for_model="'.__CLASS__.'"'),
		);
	}
	public function afterFind(){
		if(!$this->detail)
			$this->detail = new cargo_details;
		return parent::afterFind();
	}
	public function Cargo($id=0){
    	$this->getDbCriteria()->mergeWith(array(
        	'condition'=>'cargo_id=:cargo_id',
        	'params'=>array('cargo_id'=>$id),
    	));
    	return $this;
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'cargo_id' => 'Cargo',
			'item' => 'Item',
			'marking' => 'Marking',
			'country' => 'Country',
			'qty_id' => 'Qty',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('cargo_id',$this->cargo_id);

		$criteria->compare('item',$this->item,true);

		$criteria->compare('marking',$this->marking,true);

		$criteria->compare('country',$this->country,true);

		$criteria->compare('qty_id',$this->qty_id);
		
		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'pagination'=>array(
       			'pageSize'=>5,
    		),
		));
	}
}